﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public static class UnityHelper
{
    /// <summary>
    /// 查找子物体
    /// </summary>
    /// <param name="parent">父类</param>
    /// <param name="childName">子物体名字</param>
    /// <returns></returns>
    static public Transform FindChildByName(this Transform parent , string childName)
    {
        Transform tf = null;
        tf = parent.Find(childName);
        if (tf == null)
        {
            for (int i = 0; i < parent.childCount; i++)
            {
                tf = parent.GetChild(i).FindChildByName(childName);
                if (tf != null)
                {
                    return tf;
                }
            }
        }
        else
        {
            return tf;
        }
        return null;
    }
    /// <summary>
    /// 组件缺少组件
    /// </summary>
    /// <typeparam name="T">组件</typeparam>
    /// <param name="go">物体</param>
    /// <returns></returns>

    static public T AddMissComponent<T>(this GameObject go ) where T : Component
    {
        T _com = go.GetComponent<T>();
        if (_com == null)
        {
            _com = go.AddComponent<T>();
        }

        return _com;
    }

    /// <summary>
    /// 把一张图等比分割成多张图
    /// </summary>
    /// <param name="texture">目标图</param>
    /// <param name="spriteCountInTexture">等比分割的数量</param>
    /// <returns></returns>
    static public Sprite[] GetSpritesInTexture(Texture2D texture,int spriteCountInTexture)
    {
        Sprite[] _sprites = new Sprite[spriteCountInTexture];
        //每张宽度
        int _perSpriteWidth = texture.width / spriteCountInTexture; 
        for (int i = 0; i < spriteCountInTexture; i++)
        {
            _sprites[i] = Sprite.Create(texture, new Rect(new Vector2(_perSpriteWidth * i, 0), new Vector2(_perSpriteWidth, texture.height)), new Vector2(0.5f, 0.5f));
            
           
        }
        return _sprites;
    }

    // <summary>
    /// 自动转屏设置
    /// </summary>
    static public void ActiveScreenAutoRotate(bool actv)
    {
        Screen.autorotateToLandscapeLeft = actv;
        Screen.autorotateToLandscapeRight = actv;
    }

    /// <summary>
    /// 设置Image长宽
    /// </summary>
    /// <param name="iamge">imge</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    static public void SetImageRectTransform(ref Image iamge,float width,float height)
    {
        iamge.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
    }

    static public void AddButtonListener(Button pBtn, UnityAction pDele)
    {
        if (pBtn == null)
            return;
        pBtn.onClick.AddListener(pDele);
    }

    static public void RemoveButtonListener(Button pBtn, UnityAction pDele)
    {
        if (pBtn == null)
            return;

        pBtn.onClick.RemoveListener(pDele);
    }

    static public void RemoveAllButtonListener(Button pBtn)
    {
        if (pBtn == null)
            return;
        pBtn.onClick.RemoveAllListeners();
    }

    static public void AddInputFieldOnValueChangeListener(InputField pIF, UnityAction<string> pDele)
    {
        if (pIF == null)
            return;
        pIF.onValueChanged.AddListener(pDele);
    }
    static public void RemoveAllInputFieldOnValueChangeListener(InputField pIF)
    {
        if (pIF == null)
            return;
        pIF.onValueChanged.RemoveAllListeners();
    }
}
